from .getPublicData import *
import json
from myApp.models import JobInfo

def getPageData():
    return list(educations.keys())

def getAveraged(list): # 求平均薪资
    result = 0
    for i in list:
        result += i
    """
        round的作用是四舍五入。
        round(number,digits(可选))。
        number：要四舍五入的数字；
        digits：精确到小数位的数字，如果不指定，默认四舍五入到整数
    """
    return round(result / len(list), 2)

def getExperienceData(educational):
    hasEmpty = False
    if educational == '不限':
        jobs = JobInfo.objects.all()
    else:
        jobs = JobInfo.objects.filter(educational=educational)
    workExperiences = {}
    workPeople = {}
    for i in workExperience:
        workExperiences[i] = []
        workPeople[i] = 0
    for job in jobs:
        for k,v in workExperiences.items():
            if job.workExperience == k:
                if job.practice==0:
                    workExperiences[k].append(json.loads(job.salary)[1])    # 把数据库中薪资的最大值赋值给workExperiences
                    workPeople[k] += 1                                      # 工作经验计数
    for k,v in workExperiences.items():
        try:
            workExperiences[k] = getAveraged(v)
        except:
            workExperiences[k] = 0

    if len(jobs) == 0:
        hasEmpty = True
    return (workExperience,list(workExperiences.values()),list(workPeople.values()),hasEmpty)   # 这里必须要传数组

def getPeopleData():
    jobs = getAllJobs()
    educationData = {}

    # 自己写的
    # for i in educations:
    #     educationData[i] = 0
    # for job in jobs:
    #     educationData[job.educational] += 1

    # 视频教的
    for i in jobs:
        if educationData.get(i.educational,-1) == -1:
            educationData[i.educational] = 1
        else:
            educationData[i.educational] += 1

    print(list(educationData.values()),list(educationData.keys()))
    return (list(educationData.keys()),list(educationData.values()))